﻿using FlyingEye.Come;
using FlyingEye.Come.Repositories;
using FlyingEye.Exceptions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Validation;

namespace FlyingEye.ComServices
{
    public class ComeUserNotificationConfigurationService : ApplicationService
    {
        private readonly IComeUserNotificationConfigurationRepository _configurationRepository;

        public ComeUserNotificationConfigurationService(IComeUserNotificationConfigurationRepository comeUserNotificationRepository)
        {
            _configurationRepository = comeUserNotificationRepository;
        }

        public async Task<ComeUserNotificationConfigurationResponse> GetAsync(Guid id)
        {
            var model = await _configurationRepository.GetAsync(id);

            return ObjectMapper.Map<ComeUserNotificationConfigurationModel, ComeUserNotificationConfigurationResponse>(model);
        }

        public async Task<List<ComeUserNotificationConfigurationResponse>> GetListAsync()
        {
            var users = await _configurationRepository.GetListAsync();

            if (users.Any())
            {
                return ObjectMapper.Map<List<ComeUserNotificationConfigurationModel>, List<ComeUserNotificationConfigurationResponse>>(users);
            }

            return new List<ComeUserNotificationConfigurationResponse>(0);
        }

        public async Task<ComeUserNotificationConfigurationResponse> InsertAsync(CreateComeUserNotificationConfigurationRequest comeUserNotification)
        {
            if (!await _configurationRepository.AnyAsync(item => item.UniqueHashKey == comeUserNotification.UniqueHashKey))
            {
                throw new AbpValidationException("已存在相同的配置。");
            }

            if (!await _configurationRepository.AnyAsync(item => item.Name == comeUserNotification.Name))
            {
                throw new AbpValidationException($"名字[{comeUserNotification.Name}]已经被使用。");
            }

            var model = ObjectMapper.Map<CreateComeUserNotificationConfigurationRequest, ComeUserNotificationConfigurationModel>(comeUserNotification);

            model = await _configurationRepository.InsertAsync(model);

            return ObjectMapper.Map<ComeUserNotificationConfigurationModel, ComeUserNotificationConfigurationResponse>(model);
        }

        public async Task DeleteAsync(Guid id)
        {
            if (!await _configurationRepository.AnyAsync(item => item.Id == id))
            {
                throw new ResourceNotFoundException($"用户 id = {id} 配置不存在。");
            }

            await _configurationRepository.DeleteAsync(id);
        }

        public async Task<ComeUserNotificationConfigurationResponse> UpdateAsync(UpdateComeUserNotificationConfigurationRequest configuration)
        {
            if (!await _configurationRepository.AnyAsync(item => item.UniqueHashKey == configuration.UniqueHashKey))
            {
                throw new AbpValidationException("已存在相同的配置。");
            }

            if (!await _configurationRepository.AnyAsync(item => item.Name == configuration.Name))
            {
                throw new AbpValidationException($"名字[{configuration.Name}]已经被使用。");
            }

            var model = await _configurationRepository.FindAsync(configuration.Id);

            if (model == null)
            {
                throw new AbpValidationException($"Come 用户通知配置[{configuration.Id}]不存在。");
            }

            ObjectMapper.Map(configuration, model);

            model = await _configurationRepository.UpdateAsync(model);

            return ObjectMapper.Map<ComeUserNotificationConfigurationModel, ComeUserNotificationConfigurationResponse>(model);
        }
    }
}
